Find the second largest element in an array using React

Find the second largest element in an array using React

Write a function to find the second largest element in an array. The function takes an array of integers given a string as an argument and returns the second max value from the input array. If there is no second max return -1.

Also Read: PHP Array Function

Let’s take some examples:

Find the second largest element in an array

Simple Solution

The idea is to sort the array in descending order and then return the second element which is not equal to the largest element from the sorted array. Array value will be entered from the user using the input text.

Input array value: array [“5”, “5”, “4”, “2”]

Output:

second largest element

Complexity Analysis:

  • Time Complexity: O(n log n). 
    Time required to sort the array is O(n log n).
  • Auxiliary space: O(1). 
    As no extra space is required.
Also Read: How to convert array data into Simple XML file using PHP

Conclusion

Thank you for reading Find the second largest element in an array using React article. Hope I was able to help someone out. If you have any questions feel free to ask anything using the contact form. Cheers!!.

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Related posts